package org.jeecg.modules.shop.service.impl;

import org.jeecg.modules.shop.entity.*;
import org.jeecg.modules.shop.mapper.*;
import org.jeecg.modules.shop.service.IRsProductService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 商品管理
 * @Author: jeecg-boot
 * @Date:   2020-08-07
 * @Version: V1.0
 */
@Service
public class RsProductServiceImpl extends ServiceImpl<RsProductMapper, RsProduct> implements IRsProductService {

	@Resource
	private RsProductMapper rsProductMapper;
	@Resource
	private RsProductSizeMapper rsProductSizeMapper;
	@Resource
	private RsShopBargainMapper rsShopBargainMapper;
	@Resource
	private RsShopImageMapper rsShopImageMapper;
	@Resource
	private RsShopCommentMapper rsShopCommentMapper;
	
	@Override
	@Transactional
	public void saveMain(RsProduct rsProduct, List<RsProductSize> rsProductSizeList, List<RsShopBargain> rsShopBargainList, List<RsShopImage> rsShopImageList, List<RsShopComment> rsShopCommentList) {
		rsProductMapper.insert(rsProduct);
		if(rsProductSizeList!=null && rsProductSizeList.size()>0) {
			for(RsProductSize entity:rsProductSizeList) {
				//外键设置
				entity.setProductId(rsProduct.getId());
				rsProductSizeMapper.insert(entity);
			}
		}
		if(rsShopBargainList!=null && rsShopBargainList.size()>0) {
			for(RsShopBargain entity:rsShopBargainList) {
				//外键设置
				entity.setProductId(rsProduct.getId());
				rsShopBargainMapper.insert(entity);
			}
		}
		if(rsShopImageList!=null && rsShopImageList.size()>0) {
			for(RsShopImage entity:rsShopImageList) {
				//外键设置
				entity.setProductId(rsProduct.getId());
				rsShopImageMapper.insert(entity);
			}
		}
		if(rsShopCommentList!=null && rsShopCommentList.size()>0) {
			for(RsShopComment entity:rsShopCommentList) {
				//外键设置
				entity.setProductId(rsProduct.getId());
				rsShopCommentMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(RsProduct rsProduct,List<RsProductSize> rsProductSizeList,List<RsShopBargain> rsShopBargainList,List<RsShopImage> rsShopImageList,List<RsShopComment> rsShopCommentList) {
		rsProductMapper.updateById(rsProduct);
		
		//1.先删除子表数据
		rsProductSizeMapper.deleteByMainId(rsProduct.getId());
		rsShopBargainMapper.deleteByMainId(rsProduct.getId());
		rsShopImageMapper.deleteByMainId(rsProduct.getId());
		rsShopCommentMapper.deleteByMainId(rsProduct.getId());
		
		//2.子表数据重新插入
		if(rsProductSizeList!=null && rsProductSizeList.size()>0) {
			for(RsProductSize entity:rsProductSizeList) {
				//外键设置
				entity.setProductId(rsProduct.getId());
				rsProductSizeMapper.insert(entity);
			}
		}
		if(rsShopBargainList!=null && rsShopBargainList.size()>0) {
			for(RsShopBargain entity:rsShopBargainList) {
				//外键设置
				entity.setProductId(rsProduct.getId());
				rsShopBargainMapper.insert(entity);
			}
		}
		if(rsShopImageList!=null && rsShopImageList.size()>0) {
			for(RsShopImage entity:rsShopImageList) {
				//外键设置
				entity.setProductId(rsProduct.getId());
				rsShopImageMapper.insert(entity);
			}
		}
		if(rsShopCommentList!=null && rsShopCommentList.size()>0) {
			for(RsShopComment entity:rsShopCommentList) {
				//外键设置
				entity.setProductId(rsProduct.getId());
				rsShopCommentMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		rsProductSizeMapper.deleteByMainId(id);
		rsShopBargainMapper.deleteByMainId(id);
		rsShopImageMapper.deleteByMainId(id);
		rsShopCommentMapper.deleteByMainId(id);
		rsProductMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			rsProductSizeMapper.deleteByMainId(id.toString());
			rsShopBargainMapper.deleteByMainId(id.toString());
			rsShopImageMapper.deleteByMainId(id.toString());
			rsShopCommentMapper.deleteByMainId(id.toString());
			rsProductMapper.deleteById(id);
		}
	}
	
}
